# PUSH/PULL模式是一种工作队列模式，它通常用于分发任务。在该模式下，PUSH socket将任务分发给多个工作线程，工作线程使用PULL socket接收任务并处理。
import threading
import time
import zmq


def push():
    context = zmq.Context()
    socket = context.socket(zmq.PUSH)
    socket.bind("tcp://*:5555")

    for i in range(10):
        message = "Task %s" % i
        socket.send_string(message)
        time.sleep(1)


def pull(worker_id):
    context = zmq.Context()
    socket = context.socket(zmq.PULL)
    socket.connect("tcp://localhost:5555")

    while True:
        message = socket.recv_string()
        print("worker[{}]:Received task: {}".format(worker_id, message))
        time.sleep(2)



if __name__ == "__main__":
    push_thread = threading.Thread(target=push)
    push_thread.start()

    for i in range(3):
        threading.Thread(target=pull,args=(i,)).start()